package cn.telling.telMarketCentre.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import cn.telling.product.vo.TelMarketPubHis;
import cn.telling.sellerCenter.vo.SellerBasePrice;
import cn.telling.sellerCenter.vo.ShopProductAreaVo;
import cn.telling.sellercentre.dao.ISellerBasePriceDao;
import cn.telling.telMarketCentre.dao.ITelMarketCentreDao;
import cn.telling.telMarketCentre.dao.ITelMarketPubHisDao;
import cn.telling.telMarketCentre.service.ITelMarketCentreService;
import cn.telling.telMarketCentre.vo.ShopProductStockNumControlVo;
import cn.telling.utils.StringHelperTools;
/**
 * @Description:通讯市场产品中心业务处理
 * @author liugeng
 *
 */
@Service("telMarketCentreService")
public class TelMarketCentreServiceImpl implements ITelMarketCentreService {
	@Autowired
	@Qualifier("telMarketCentreDao")
	private ITelMarketCentreDao telMarketCentreDao;
	public ITelMarketCentreDao getTelMarketCentreDao() {
		return telMarketCentreDao;
	}
	public void setTelMarketCentreDao(ITelMarketCentreDao telMarketCentreDao) {
		this.telMarketCentreDao = telMarketCentreDao;
	}
	@Autowired
	@Qualifier("telMarketPubHisDao")
	private ITelMarketPubHisDao telMarketPubHisDao;
	public ITelMarketPubHisDao getTelMarketPubHisDao() {
		return telMarketPubHisDao;
	}
	public void setTelMarketPubHisDao(ITelMarketPubHisDao telMarketPubHisDao) {
		this.telMarketPubHisDao = telMarketPubHisDao;
	}
	@Autowired
	@Qualifier("sellerBasePriceDao")
	private ISellerBasePriceDao sellerBasePriceDao;
	public ISellerBasePriceDao getSellerBasePriceDao() {
		return sellerBasePriceDao;
	}
	public void setSellerBasePriceDao(ISellerBasePriceDao sellerBasePriceDao) {
		this.sellerBasePriceDao = sellerBasePriceDao;
	}
	
	/**通讯市场产品已上架、已下架产品  通讯市场卖家*/
	@Override
	public List<ShopProductAreaVo> getTelMarketSaleListByStatus(String userId,String status, String conditions) {
		return telMarketCentreDao.getTelMarketSaleListByStatus(userId, status, conditions);
	}
	
	/**通讯市场产品已上架、已下架产品   临时卖家*/
	@Override
	public List<ShopProductAreaVo> getTempSaleSaleListByStatus(String userId,String status, String conditions) {
		return telMarketCentreDao.getTempSaleSaleListByStatus(userId, status, conditions);
	}
	
	/**修改通讯市场产品区域信息*/
	@Override
	public int updateShopProductAreaInfo(ShopProductAreaVo shopProductAreaVo, String userId, String status, String isTempSale) {
		String supplyAreaId = String.valueOf(shopProductAreaVo.getSupplyAreaId());
		
		ShopProductAreaVo currentShopProductAreaVo = null;
		if("0".equals(isTempSale)){
			currentShopProductAreaVo = telMarketCentreDao.getTempSaleShopProductAreaVoById(supplyAreaId, userId, status);
		}else {
			currentShopProductAreaVo = telMarketCentreDao.getTelMarketShopProductAreaVoById(supplyAreaId, userId, status);
		}
		
		boolean hasChange = false;
		String remark = "";
		
		/**有价格修改*/
		boolean priceFlag = false;
		BigDecimal currentPrice = currentShopProductAreaVo.getPriceRetailOnline().setScale(2,BigDecimal.ROUND_HALF_EVEN);
		BigDecimal modifyPrice = shopProductAreaVo.getPriceRetailOnline().setScale(2, BigDecimal.ROUND_HALF_EVEN);
		if (!currentPrice.equals(modifyPrice)) {
			remark = "修改价格为:" + modifyPrice.toString();
			hasChange = true;
			priceFlag = true;
		}
		
		/**有库存修改*/
		BigDecimal currentStockNumber = currentShopProductAreaVo.getStockNumber().setScale(2,BigDecimal.ROUND_HALF_EVEN);
		BigDecimal modifyStockNumber = shopProductAreaVo.getStockNumber().setScale(2,BigDecimal.ROUND_HALF_EVEN);
		if(!currentStockNumber.equals(modifyStockNumber)){
			if (hasChange) {
				/**有价格修改*/
				remark += ",";
			}
			remark += "库存修改为:" + String.valueOf(modifyStockNumber);
			
			hasChange = true;
		}
		
		/**售后修改*/
		String currentAfterSale = StringHelperTools.nvl(currentShopProductAreaVo.getAfterSale());
		String modifyAfterSale = StringHelperTools.nvl(shopProductAreaVo.getAfterSale());
		if(!currentAfterSale.equals(modifyAfterSale)){
			if (hasChange) {
				/**有过价格和库存修改*/
				remark += ",";
			}
			remark += "售后修改为:"+modifyAfterSale;
			
			hasChange = true;
		}
		
		/**赠品修改*/
		String currentGifts = StringHelperTools.nvl(currentShopProductAreaVo.getGifts());
		String modifyGifts = StringHelperTools.nvl(shopProductAreaVo.getGifts());
		if(!currentGifts.equals(modifyGifts)){
			if (hasChange) {
				/**有过价格、库存、售后修改*/
				remark += ",";
			}
			remark += "赠品修改为:"+modifyGifts;
			
			hasChange = true;
		}
		
		if(hasChange){
			shopProductAreaVo.setRemark(remark);
			//更新价格和库存
			int count = telMarketCentreDao.updateShopProductAreaInfo(shopProductAreaVo,userId);
			//更新截止日期
			telMarketPubHisDao.updateTelMarketPubHis(String.valueOf(shopProductAreaVo.getProductId()), String.valueOf(shopProductAreaVo.getSupplyAreaId()));
			//更新基准价格
			if(priceFlag){
				sellerBasePriceDao.updateSellerBasePrice(shopProductAreaVo.getPriceRetailOnline(), new BigDecimal(userId), String.valueOf(shopProductAreaVo.getSupplyId()), String.valueOf(shopProductAreaVo.getProductId()));
			}
			//记录发布历史
			TelMarketPubHis telMarketPubHis = new TelMarketPubHis();
			telMarketPubHis.setProduct_id(shopProductAreaVo.getProductId());
			telMarketPubHis.setSupplyId(shopProductAreaVo.getSupplyId());
			telMarketPubHis.setSupplyAreaId(shopProductAreaVo.getSupplyAreaId());
			telMarketPubHis.setPrice(shopProductAreaVo.getPriceRetailOnline());
			telMarketPubHis.setStockNum(shopProductAreaVo.getStockNumber());
			telMarketPubHis.setAdd_user(new BigDecimal(userId));
			telMarketPubHis.setUpdate_user(new BigDecimal(userId));
			telMarketPubHisDao.insertTelMarketPubHis(telMarketPubHis);
			
			return count;
		}
		
		return 0;
	}
	
	/**修改通讯市场产品区域信息   上架状态*/
	@Override
	public int updateShopProductAreaOnSale(List<ShopProductAreaVo> shopProductAreaVoList, String status, String userId) {
		//上架  批量更新价格和库存
		int count = telMarketCentreDao.updateShopProductAreaOnSale(shopProductAreaVoList, status, userId);
		
		List<SellerBasePrice> sellerBasePriceList = new ArrayList<SellerBasePrice>();
		List<TelMarketPubHis> telMarketPubHisList = new ArrayList<TelMarketPubHis>();
		for(ShopProductAreaVo shopProductAreaVo : shopProductAreaVoList){
			SellerBasePrice sellerBasePrice = new SellerBasePrice();
			sellerBasePrice.setBasePrice(shopProductAreaVo.getPriceRetailOnline());
			sellerBasePrice.setAdd_user(new BigDecimal(userId));
			sellerBasePrice.setSupplyId(shopProductAreaVo.getSupplyId());
			sellerBasePrice.setProductId(shopProductAreaVo.getProductId());
			sellerBasePriceList.add(sellerBasePrice);
			TelMarketPubHis telMarketPubHis = new TelMarketPubHis();
			telMarketPubHis.setProduct_id(shopProductAreaVo.getProductId());
			telMarketPubHis.setSupplyId(shopProductAreaVo.getSupplyId());
			telMarketPubHis.setSupplyAreaId(shopProductAreaVo.getSupplyAreaId());
			telMarketPubHis.setPrice(shopProductAreaVo.getPriceRetailOnline());
			telMarketPubHis.setStockNum(shopProductAreaVo.getStockNumber());
			telMarketPubHis.setAdd_user(new BigDecimal(userId));
			telMarketPubHis.setUpdate_user(new BigDecimal(userId));
			
			telMarketPubHisList.add(telMarketPubHis);
		}
		//批量更新基准价格
		sellerBasePriceDao.updateSellerBasePriceBatch(sellerBasePriceList);
		//批量记录发布历史
		telMarketPubHisDao.insertTelMarketPubHis(telMarketPubHisList);
		return count;
	}
	
	/**修改通讯市场产品区域信息   下架状态*/
	@Override
	public int updateShopProductAreaOffSale(List<String> supplyAreaIdList, String status, List<String> productIdList, String userId) {
		//下架
		int count = telMarketCentreDao.updateShopProductAreaOffSale(supplyAreaIdList,status,userId);
		//更新截止日期
		telMarketPubHisDao.updateTelMarketPubHisBatch(productIdList, supplyAreaIdList);
		
		return count;
	}
	
	/**获得库存管控信息  供发短信使用*/
	@Override
	public List<ShopProductStockNumControlVo> getShopProductStockNumControlVoListBySms(int topValue) {
		return telMarketCentreDao.getShopProductStockNumControlVoListBySms(topValue);
	}
	
	/**获得库存管控信息  供自动下架处理*/
	@Override
	public List<ShopProductStockNumControlVo> getShopProductStockNumControlVoListByOff(int topValue) {
		return telMarketCentreDao.getShopProductStockNumControlVoListByOff(topValue);
	}
}
